package com.zpf.dao.impl;

import com.zpf.dao.AddressDao;
import com.zpf.pojo.Address;
import com.zpf.utils.DataSourceUtils;
import com.zpf.utils.TxUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.lang.model.element.VariableElement;
import java.rmi.server.UID;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class AddressDaoImpl implements AddressDao {
    @Override
    public List<Address> findAddListByUid(int uid) {
        QueryRunner qf = new QueryRunner(DataSourceUtils.getDataSource());

        String sql = "select id , detail , name , phone , uid , level from tb_address where uid = ? ";
        try {
            List<Address> addList = qf.query(sql, new BeanListHandler<>(Address.class), uid);
            return addList;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询地址列表失败");
        }
        return null;
    }

    @Override
    public void insert(Address address) {

        String sql = "insert into `tb_address` (id , detail , name , phone ,uid ,level) values (null,?,?,?,?,?)";
        QueryRunner qf = new QueryRunner(DataSourceUtils.getDataSource());
        try {
            qf.update(sql, address.getDetail(), address.getName(), address.getPhone(), address.getUid(), address.getLevel());
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("新增失败");
        }

    }

    @Override
    public void deleteById(String id) {

        String sql = "delete from tb_address where id = ?";
        QueryRunner qf = new QueryRunner(DataSourceUtils.getDataSource());
        try {
            qf.update(sql, id);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("删除失败");

        }
    }

    @Override
    public void update(Address address) {
        String sql = "UPDATE `tb_address` SET `detail`=?, `name`=?, `phone`=?, `uid`=?, `level`=? WHERE (`id`=?)";
        QueryRunner qf = new QueryRunner(DataSourceUtils.getDataSource());
        try {
            qf.update(sql, address.getDetail(), address.getName(), address.getPhone(), address.getUid(), address.getLevel(), address.getId());
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("修改失败");
        }
    }

    @Override
    public void clearDefaultAddress( int uid) {
        Connection conn = TxUtils.getConn();
        System.out.println(conn);
        String sql = "UPDATE `tb_address` SET level=0 WHERE (uid = ?)";
        QueryRunner qf = new QueryRunner(DataSourceUtils.getDataSource());
        try {
            qf.update(sql, uid);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("修改失败");
        }
    }

    @Override
    public void setDefaultAddress(String aid) {
        Connection conn = TxUtils.getConn();
        System.out.println(conn);
        String sql = "UPDATE `tb_address` SET level=1 WHERE (id = ?)";
        QueryRunner qf = new QueryRunner(DataSourceUtils.getDataSource());
        try {
            qf.update(sql,aid);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("修改失败");
        }
    }
}